package demo4.controller;

import com.tedu.InputUtil;
import demo4.mapper.IUserMapper;
import demo4.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Scanner;

@Controller
public class UserController {

    @Autowired
    private IUserMapper userMapper;

    public void start(){
        System.out.println("请选择对应的操作，输入序号：");
        System.out.println("1. 注册, 2. 登录, 3. 修改, 4. 查询, 5. 删除, 6. 退出");
        Scanner scanner = new  Scanner(System.in);
        int num = Integer.parseInt(scanner.nextLine());
        switch (num){
            case 1:
                regUser();
                break;
            case 2:
                loginUser();
                break;
            case 3:
                update();
                break;
            case 4:
                UserList();
                break;
            case 5:
                deleteUser();
                break;
            default:
                System.out.println("其他功能正在完善中，404。。。");
        }
    }

    public void regUser(){

        /*System.out.println("欢迎注册");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = scanner.nextLine();
        System.out.println("请输入密码");
        String password = scanner.nextLine();
        System.out.println("请输入昵称");
        String nickname = scanner.nextLine();
        System.out.println("请输入年龄");
        int age = scanner.nextInt();*/
        User user = InputUtil.getInputObject(new User());

        String username = user.getUsername();
        if(userMapper.selectByUsername(username) != null){
            System.out.println("用户已存在！");
        }else{
            int number = userMapper.insert(user);
            System.out.println(number > 0 ? "注册成功":"注册失败");
        }
    }

    public void loginUser(){

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = scanner.nextLine();
        System.out.println("请输入密码");
        String password = scanner.nextLine();
        User user = userMapper.selectByUsername(username);
        if(user == null){
            System.out.println("用户不存在！");
        }else{
            if(user.getPassword().equals(password)){
                System.out.println("登录成功，欢迎【"+user.getNickname()+"】回来");
            }else{
                System.out.println("密码错误!!!");
            }
        }
    }

    public void update(){
        User user = InputUtil.getInputObject(new User());
        int number = userMapper.update(user);
        System.out.println(number > 0 ? "修改成功":"修改失败");
    }

    public void UserList(){

        System.out.println("查询所有用户：");
        List<User> userList = userMapper.selectAll();
        userList.forEach(System.out::println);
    }

    public void deleteUser(){

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除的用户名");
        String username = scanner.nextLine();
        int number = userMapper.deleteByUsername(username);
        System.out.println(number > 0? "删除成功":"删除失败");
    }
}
